package com.qys.livesMall.app.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qys.livesMall.app.user.model.dto.sys.LoginInfoDTO;
import com.qys.livesMall.app.user.model.dto.sys.UserQuery;
import com.qys.livesMall.app.user.model.entity.UserShopStatistics;
import com.qys.livesMall.app.user.mapper.UserShopStatisticsMapper;
import com.qys.livesMall.app.user.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qys.livesMall.framework.utils.SecurityUtils;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 店铺和会员通用统计表 服务实现类
 * </p>
 *
 * @author
 * @since 2023-08-01
 */
@Service
public class UserShopStatisticsServiceImpl extends ServiceImpl<UserShopStatisticsMapper, UserShopStatistics> implements UserShopStatisticsService {

    @Resource
    private IH2AppUserService appUserService;
    @Override
    public UserShopStatistics getbyUserName(String superior) {
        return baseMapper.getbyUserName(superior);
    }

    @Override
    public Integer updateUserIP(LoginInfoDTO loginInfoDTO) {
        //查询活跃人数
        Integer count = appUserService.getActivebyLastLoginTime(loginInfoDTO.getIp());

        LambdaQueryWrapper<UserShopStatistics> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(UserShopStatistics::getUserId,loginInfoDTO.getUserId());
        List<UserShopStatistics> list = list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)){
            UserShopStatistics userShopStatistics=list.get(0);
            userShopStatistics.setFinalOperateIp(loginInfoDTO.getIp());
            userShopStatistics.setFinalOperateTime(loginInfoDTO.getNow());
            userShopStatistics.setFinalLanguage(SecurityUtils.getLanguage());
            userShopStatistics.setIpActiveNumber(count);
            updateById(userShopStatistics);
        }
        if (null != count && null != loginInfoDTO.getIp()){
            LambdaUpdateWrapper<UserShopStatistics> updateWrapper=new LambdaUpdateWrapper<>();
            updateWrapper.eq(UserShopStatistics::getFinalOperateIp,loginInfoDTO.getIp());
            updateWrapper.set(UserShopStatistics::getIpActiveNumber,count);
            update(updateWrapper);
        }
        return 1;
    }

    @Override
    public UserShopStatistics selectByUserId(Long userId) {
        return baseMapper.selectByUserId(userId);
    }

    @Override
    public List<UserShopStatistics> getUserShopStatisticsAll() {
        return baseMapper.getUserShopStatisticsAll();
    }

    @Override
    public List<Long> getUserId(UserQuery query) {
        boolean exc=false;
        LambdaQueryWrapper<UserShopStatistics> wq =new LambdaQueryWrapper<>();
        wq.select(UserShopStatistics::getUserId);

        if (query.getLongUserIds()!=null){
            if (query.getLongUserIds().size()==0){
                query.getLongUserIds().add(-1L);
            }
            wq.in(UserShopStatistics::getUserId,query.getLongUserIds());
            exc=true;
        }

        if (StringUtils.isNotBlank(query.getSupUserName())){
            wq.eq(UserShopStatistics::getSuperior, query.getSupUserName());
            exc=true;
        }
        if (StringUtils.isNotBlank(query.getPoxUserName())){
            wq.eq(UserShopStatistics::getAgentAccount, query.getPoxUserName());
            exc=true;
        }
        if (query.getStorey()!=null){
            wq.eq(UserShopStatistics::getStorey,query.getStorey());
            exc=true;
        }
        if (!exc){
            return null;
        }
        List<UserShopStatistics> list=list(wq);
        if (CollectionUtils.isNotEmpty(list)){
            return list.stream().map(UserShopStatistics::getUserId).toList();
        }
        return new ArrayList<>();
    }

    @Override
    public List<UserShopStatistics> listByUserIds(List<Long> userIds) {
        LambdaQueryWrapper<UserShopStatistics> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(UserShopStatistics::getUserId,userIds);
        return list(queryWrapper);
    }

    @Override
    public List<Long> getShopIdsByTeamId(List<Long> list) {
        return baseMapper.getShopIdsByTeamId(list);
    }

}
